<?php
define("DIR_ROOT", dirname(__FILE__));

$source =  require 'a.php';

$source = preparSource($source);

//print_r($source);die;

// 生成 DOM 解析类
$dom = new DOMDocument('1.0', 'utf-8');
        
if (!@$dom->loadHTML('<?xml encoding="utf-8">'.$source)) {
  echo 11;
}

            
           foreach ($dom->childNodes as $item) {
                if ($item->nodeType == XML_PI_NODE) {
                    $dom->removeChild($item); // remove hack
                }
            }

        $parentNodeBox = array();    
        $parentNodes = array();
        $parentIDS = array();
        
        $allParagraphs = $dom->getElementsByTagName("p");
		
        
        foreach ($allParagraphs as $k=>$node){
        	$parentNode   = $node->parentNode; 
            $className    = $parentNode->getAttribute("class");
            $id           = $parentNode->getAttribute("id");
            if($className){ $parentNodes[$k] = $className ;} 
            if($id){ $parentIDS[$k] = $id ;} 

                    
            if (strlen($node->nodeValue) > 10) {
                $contentScore += strlen($paragraph->nodeValue);
            }
            // 保存章节的父元素，以便下次快速获取
            array_push($parentNodeBox, $parentNode);
        }
die;
        $topBox = null;
        
        $truenode = $a = $b = array();
        if(!empty($parentNodes)){
	        $a = array_count_values($parentNodes); 
	        arsort($a);  var_dump($a);
	        $akeys = array_keys($a);//倒序排序       
	        $akey = $akeys[0]; 
            $truenode[$akey.'_IKCL'] = $a[$akey];
        }
        if(!empty($parentIDS)){
	   		$b = array_count_values($parentIDS);
	        arsort($b); var_dump($b);
	        $bkeys = array_keys($b);//倒序排序
	        $bkey = $bkeys[0];
	        $truenode[$bkey.'_IKID'] = $b[$bkey];
        }
		//
		die;
        arsort($truenode);
        $truenode = array_keys($truenode);
        $truenode   = $truenode[0];
        if($p = strpos($truenode,'_IKCL')){
        	$topBox = array('IKCL'=>substr($truenode, 0,$p));
        }elseif($p = strpos($truenode,'_IKID')){
        	$topBox = array('IKID'=>substr($truenode, 0,$p));
        }
        
        //返回
        $realNode = null;
        foreach ($parentNodeBox as $key=>$node){
        	if(!empty($topBox['IKID'])){ 
        		 $id  = $node->getAttribute("id"); 
        		 if($id == $topBox['IKID']){
        		 	$realNode = $node;
        		 	break;
        		 }
        	}
                	
        	if(!empty($topBox['IKCL'])){ 
        		 $class  = $node->getAttribute("class"); 
        		 if($class == $topBox['IKCL']){
        		 	$realNode = $node;
        		 	break;
        		 }
        	}        	
        }
        
        print_r($realNode->nodeValue);die;
        
        die;
function preparSource($string) {
		//匹配 <meta charset="utf-8">
        // 剔除多余的 HTML 编码标记，避免解析出错
        preg_match("/charset=(\"[\w|\-]+\");?/", $string, $match); 
        if (isset($match[1])) {
            $string = preg_replace("/charset=(\"[\w|\-]+\");?/", "", $string, 1);
        }

        preg_match("/charset=([\w|\-]+);?/", $string, $match); 
        //匹配<meta http-equiv="content-type" content="text/html;charset=GB2312">
        if (isset($match[1])) {
            $string = preg_replace("/charset=([\w|\-]+);?/", "", $string, 1);
        }        
        
       
        // Replace all doubled-up <BR> tags with <P> tags, and remove fonts.
        $string = preg_replace("/<br\/?>[ \r\n\s]*<br\/?>/i", "</p><p>", $string);
        $string = preg_replace("/<\/?font[^>]*>/i", "", $string);

        
        // @see https://github.com/feelinglucky/php-readability/issues/7
        //   - from http://stackoverflow.com/questions/7130867/remove-script-tag-from-html-content
        $string = preg_replace("#<script(.*?)>(.*?)</script>#is", "", $string);

        //替换注释
        $string = preg_replace('/<!--(.*?)[ \r\n\s]*-->/', '', $string);
        //去除iframe
        $string = preg_replace('#<iframe(.*?)>(.*?)</iframe>#is', '', $string);
        //去除<style></style>
        $string = preg_replace('#<style>(.*?)</style>#is', '', $string); 

        //去除HTML属性
       // $string = preg_replace('/<([a-z]+)\s+[^>]*>/is', '<$1>', $string);
        
        return trim($string);
}
